package com.msameer.gwttutor.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.editor.client.adapters.EditorSource;
import com.google.gwt.editor.client.adapters.ListEditor;
//import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
//import com.msameer.gwtdemo.client.CustomersView.Presenter;
import com.msameer.gwttutor.server.Customer;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;

public class CustomersViewImpl extends Composite implements CustomersView{

	private static CustomersViewImplUiBinder uiBinder = GWT
			.create(CustomersViewImplUiBinder.class);
	@UiField FlowPanel container;
	@UiField(provided=true) CellTable<Customer> cellTable = new CellTable<Customer>();

	//private Presenter presenter;
	//private EventBus eventBus;

	interface CustomersViewImplUiBinder extends
			UiBinder<Widget, CustomersViewImpl> {
	}

	interface Driver extends SimpleBeanEditorDriver<List<Customer>, ListEditor<Customer, CustomerEditor>> {}

	/**
	 * This is used by the ListEditor to control the state of the UI.
	 */
	private class CustomerEditorSource extends EditorSource<CustomerEditor> {
		@Override
		public CustomerEditor create(int index) {
			CustomerEditor customerEditor = new CustomerEditor(/*eventBus*/);
			//customerEditor.setStylePrimaryName(style.favorite());
			container.insert(customerEditor, index);
			return customerEditor;
		}

		@Override
		public void dispose(CustomerEditor subEditor) {
			subEditor.removeFromParent();
			//subEditor.cancelSubscription();
		}

		@Override
		public void setIndex(CustomerEditor editor, int index) {
			container.insert(editor, index);
		}
	}
	
	public CustomersViewImpl(/*EventBus eventBus*/) {
		initWidget(uiBinder.createAndBindUi(this));
	    //---------------------------------------------- Card View -----------------------------------	
		//this.eventBus = eventBus;
	    // Create the driver which manages the data-bound widgets
	    Driver driver = GWT.<Driver> create(Driver.class);

	    // Use a ListEditor that uses our NameLabelSource
	    ListEditor<Customer, CustomerEditor> editor = ListEditor.of(new CustomerEditorSource());

	    // Configure the driver
	    ListEditor<Customer, CustomerEditor> listEditor = editor;
	    //driver.initialize(eventBus, factory, listEditor);
	    driver.initialize(listEditor);

	    // Notice the backing list is essentially anonymous.
	    driver.edit(Customer.getAll());

	    // Modifying this list triggers widget creation and destruction
	    //displayedList = listEditor.getList();
	    //---------------------------------------------- Grid View -----------------------------------
	    // Create Id column.
	    TextColumn<Customer> idColumn = new TextColumn<Customer>() {
	      @Override
	      public String getValue(Customer customer) {
	        return customer.getId().toString();
	      }
	    };

	    // Create name column.
	    TextColumn<Customer> nameColumn = new TextColumn<Customer>() {
	      @Override
	      public String getValue(Customer customer) {
	        return customer.getName();
	      }
	    };

	    // Add the columns.
	    cellTable.addColumn(idColumn, "Id");
	    cellTable.addColumn(nameColumn, "Name");

	    // Set the total row count. This isn't strictly necessary, but it affects
	    // paging calculations, so its good habit to keep the row count up to date.
	    cellTable.setRowCount(Customer.getAll().size(), true);

	    // Push the data into the widget.
	    cellTable.setRowData(0, Customer.getAll());	    
	}

	@Override
	public void setPresenter(Presenter presenter) {
		//this.presenter = presenter;
		
	}

	@Override
	protected void onLoad() {
	    // Subscribe to notifications from the FavoritesManager
//	    subscription = manager.addMarkFavoriteHandler(new MarkFavoriteEvent.Handler() {
//	      public void onMarkFavorite(MarkFavoriteEvent event) {
//	        FavoritesWidget.this.onMarkFavorite(event);
//	      }
//	    });

	    // Initialize the UI with the existing list of favorites
	    //for (EntityProxyId<PersonProxy> id : manager.getFavoriteIds()) {
	    //  onMarkFavorite(new MarkFavoriteEvent(id, true));
	    //}
	  }

//	  @Override
//	  protected void onUnload() {
//	    subscription.removeHandler();
//	  }	
}
